<?php
/**
 * The template for displaying all pages
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages and that other
 * 'pages' on your WordPress site will use a different template.
 *
 * @package WordPress
 * @subpackage Twenty_Thirteen
 * @since Twenty Thirteen 1.0
 */

if(isset($_GET['args']))
{

	if($_GET['args'] == 2 && $_GET['url']){
	//	echo '<div class="fb-like" data-href=' . $_GET['url'] . ' data-layout="standard" data-action="like" data-show-faces="true" data-share="true"></div>';
	//	echo '<div id="fb-root"></div> <script>(function(d, s, id){var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=401509169952372"; fjs.parentNode.insertBefore(js, fjs); }(document, "script", "facebook-jssdk"));</script>';
	//	echo 'test<div class="fb-like" data-href="https://www.facebook.com/'.$_GET['url'].'" data-layout="standard" data-action="like" data-show-faces="true" data-share="true"></div>';
	//	echo file_get_contents('http://www.facebook.com/plugins/like.php?href='. urlencode($_GET['url']).'&width&layout=standard&action=like&show_faces=true&share=true&height=80&appId=401509169952372');	
	//	echo 'http://www.facebook.com/plugins/like.php?href='. urlencode($_GET['url']).'&width&layout=standard&action=like&show_faces=true&share=false&height=80&appId=401509169952372';	
		echo 'http://www.facebook.com/plugins/likebox.php?href='. urlencode($_GET['url']) . '&width&height=62&colorscheme=light&show_faces=false&header=false&stream=false&show_border=false&appId=401509169952372';
	//echo $_GET['url'];
	}
	elseif($_GET['args'] == 1){
		print_r($_GET);
		echo '<script>alert(\'test\');</script>sumulpot ka!';
	}elseif($_GET['args'] == "wpdbtest_getid"){
		$result = $wpdb->get_row("SELECT * FROM wp_allowablepoints ORDER BY AP_ID DESC");
		echo ((int) $result->AP_ID) + 11;
	}elseif($_GET['args'] == "sync_twitter")
	{
		$points = $_GET['points'];
		$tumbler_username = $_GET['uname'];
		
		require_once ('codebird-php/src/codebird.php');
		
		$consumerKey = 'ycItkf9sKkEDeSc9XuP3XA';
		$consumerSecret = 'EMMxVY9zLeZm0v0OhC4CxN9chjs9sCaadUOHVwaL8bc';
		
		$cb = \Codebird\Codebird::getInstance();
		$cb->setConsumerKey($consumerKey, $consumerSecret);
		
		try{
			$reply = (array) $cb->users_show('screen_name=' . $_GET['uname']);
		}
		catch(Exception $e){
			print_r( 'Error on getting Twitter information: ' . $e->getMessage());
		}
		
		if ($reply['httpstatus'] == 200 || $reply['httpstatus'] == '200')
		{
			try {
				//$wpdb->query(
				//		$wpdb->prepare(
				//						"INSERT INTO gvk_url_list 
				//						(user_id, site_id, url_name, date_added, points_to_give) 
				//						VALUES ( %d, %d, %s, %s, %d )", 
				//						array (1, 1, $reply['screen_name'], (string) date("d/m/Y"), (int) $points)
				//		));
				print_r('Good job! You have successfully added your Twitter to your HitStash! Sit back and watch your profile gain more followers. Want to add more reward Coins? Click here.');
			}
			catch(Exception $e){
				print_r( 'Error on saving to database: ' . $e->getMessage());
			}
		}
		else
		{
			print_r('Failed. Username does not exist.');
		}
		/*<iframe allowtransparency="true" frameborder="0" scrolling="no"
		src="//platform.twitter.com/widgets/follow_button.html?screen_name=<?php echo $_GET['uname'];?>"
		style="width:300px; height:20px;"></iframe>*/
	}
	elseif($_GET['args'] == "sync_tumbler")
	{
		$tumbler_username = $_GET['uname'] . '.tumblr.com';
		$points = $_GET['points'];
		
		include 'tumblr-php/vendor/autoload.php';
		
		$consumerKey = 'bi86VYLsU578l8urQVuDAzqVV90cVQ3mOHaFkMBughNyKUW4l4';
		$consumerSecret = 'riy9EE4QJpr4bRfCK2YNuiyYuP4WNifDuRnb99f6Jk6sEnaEEI';
		
		$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
		
		try {
			$reply = $client->getBlogInfo($tumbler_username);
		}
		catch(Exception $e){
			print_r( 'Error on getting Tumblr Blog information: ' . $e->getMessage());
			return;
		}
		
		try{
			//$wpdb->query(
			//			$wpdb->prepare(
			//							"INSERT INTO gvk_url_list 
			//							(user_id, site_id, url_name, date_added, points_to_give) 
			//							VALUES ( %d, %d, %s, %s, %d )", 
			//							array (1, 1, $reply->blog->name, (string) date("d/m/Y"), (int) $points)
			//			));
			print_r('Good job! You have successfully added your Tumblr to your HitStash! Sit back and watch your profile gain more followers. Want to add more reward Coins? Click here.');
		}
		catch(Exception $e){
			print_r( 'Error on saving to database: ' . $e->getMessage());
		}
	}
	elseif($_GET['args'] == "sync_website")
	{
		$blogger_url = $_GET['url'];
		$points = $_GET['points'];
		$result = null;
		try{
			$headers = get_headers($blogger_url);
			$h = explode(' ', $headers[0], 2);
			$result = $headers ? intval(end($h)) : false;
		}
		catch(Exception $e){
			print_r( 'Error validating ' . $_GET['type'] . ' information: ' . $e->getMessage());
			return;
		}
		if ($result == 200 || $result == '200')
		{
			try{
				print_r('Good job! You have successfully added your ' . $_GET['type'] . ' to your HitStash! Sit back and watch your profile gain more hits. Want to add more reward Coins? Click here.');
			}
			catch(Exception $e){
				print_r( 'Error on saving to database: ' . $e->getMessage());
				return;
			}
		}
		else
		{
			print_r('Please copy the link address of your ' . $_GET['type'] . ' then paste it to the provided box.');
		}
	}
}
else
{

get_header(); ?>

	<div id="primary" class="content-area">
		<div id="content" class="site-content" role="main">

			<?php /* The loop */ ?>
			<?php while ( have_posts() ) : the_post(); ?>

				<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
					<header class="entry-header">
						<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
						<div class="entry-thumbnail">
							<?php the_post_thumbnail(); ?>
						</div>
						<?php endif; ?>

						<h1 class="entry-title"><?php the_title(); ?></h1>
					</header><!-- .entry-header -->

					<div class="entry-content">
						<?php the_content(); ?>
						<?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
					</div><!-- .entry-content -->

					<footer class="entry-meta">
						<?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
					</footer><!-- .entry-meta -->
				</article><!-- #post -->

				<?php comments_template(); ?>
			<?php endwhile; ?>

		</div><!-- #content -->
	</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

<?php
}
?>
